package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import models.RatingBest;
import models.RatingNom;
import models.RatingRegion;
import models.SolutionSubmission;
import models.SolutionSubscription;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

public class ajxRemoveSolutionSubscriptionServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			//resp.setContentType("text/xml");
			PrintWriter out = resp.getWriter();
	        UserService userService = UserServiceFactory.getUserService();
	        String userEmailID = userService.getCurrentUser().getEmail().toLowerCase(); 
	        
	        /*
	        String parameter;
	        Enumeration parameterNames;
	        for( parameterNames = req.getParameterNames() ; parameterNames.hasMoreElements() ; ){
	            parameter = parameterNames.nextElement().toString();
	           out.println("[ "+parameter+" ] = "+req.getParameterValues(parameter)[0]+"<br/>");            
	       }	        
	         */
	        
			String KeyID = req.getParameter("Key");
			if ( KeyID == null ){
				resp.sendRedirect("/modules/default/notifyme.jsp?error=KeyID is null.");
			}			

			String CatID = req.getParameter("Cat");
			if ( CatID == null ){
				resp.sendRedirect("/modules/default/notifyme.jsp?error=CatID is null.");
			}
			
			String temp[] = KeyID.split("\\~");			
			long tempID = 0;
			Query<SolutionSubscription> q = null;
			
			if ( CatID.equalsIgnoreCase("CustomersInfo") ) {
				Objectify ofy = ObjectifyService.begin();
				
				for(int i = 0; i < temp.length; i++){
					tempID = Long.parseLong(temp[i]);
					q = ofy.query(SolutionSubscription.class).filter("custID", tempID).filter("subscribeBY", userEmailID);
			   		for ( SolutionSubscription f : q.fetch() ) {			    			
			    			ofy.delete(f);
			   		}					
				}				
			}else if ( CatID.equalsIgnoreCase("ProcessInfo") ) {
				Objectify ofy = ObjectifyService.begin();
				
				for(int i = 0; i < temp.length; i++){
					tempID = Long.parseLong(temp[i]);					
					q = ofy.query(SolutionSubscription.class).filter("areaID", tempID).filter("subscribeBY", userEmailID);
			   		for ( SolutionSubscription f : q.fetch() ) {			    			
			    			ofy.delete(f);
			   		}					
					
				}				
			}else if ( CatID.equalsIgnoreCase("BenefitsInfo") ) {
				Objectify ofy = ObjectifyService.begin();
				
				for(int i = 0; i < temp.length; i++){
					tempID = Long.parseLong(temp[i]);					
					q = ofy.query(SolutionSubscription.class).filter("benefitID", tempID).filter("subscribeBY", userEmailID);
			   		for ( SolutionSubscription f : q.fetch() ) {			    			
			    			ofy.delete(f);
			   		}				
					
				}				
			}else if ( CatID.equalsIgnoreCase("SitesInfo") ) {
				Objectify ofy = ObjectifyService.begin();
				
				for(int i = 0; i < temp.length; i++){
					tempID = Long.parseLong(temp[i]);					
					q = ofy.query(SolutionSubscription.class).filter("locID", tempID).filter("subscribeBY", userEmailID);
			   		for ( SolutionSubscription f : q.fetch() ) {			    			
			    			ofy.delete(f);
			   		}
				}				
			}
	        
			resp.sendRedirect("/modules/default/notifyme.jsp?Key="+CatID);
			//out.println("UnSubscribed successfully");
			
			
		} catch(Exception ee){
			ee.printStackTrace();
		}
	}
}
